package com.gpscookie.mobile;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;

import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;

import de.enough.polish.ui.Alert;
import de.enough.polish.ui.AlertType;
import de.enough.polish.ui.Item;
import de.enough.polish.ui.ItemCommandListener;
import de.enough.polish.ui.StringItem;
import de.enough.polish.util.Locale;

public class ResultScreen extends BasicScreen implements DownloadListener, ItemCommandListener
{

	private DownloadManager dlManager = null;
	private int offset = 0;
	private int limit = 6;
	private String mapurl = null;
	private Command cmdMap;
	private Command cmdView;
	
	public ResultScreen(String title, GPSCookie globalClass) {
		super(title, globalClass);
		this.dlManager = new DownloadManager();
		this.cmdBack = new Command(Locale.get("BACK"), Command.SCREEN, 20);		
		this.cmdMap = new Command(Locale.get("MAP"), Command.SCREEN, 10);
		this.addCommand(this.cmdMap);
		this.addCommand(this.cmdBack);	
		this.parentScreen = GPSCookie.mainScreen;		
		this.appendAd();
		this.cmdView = new Command(Locale.get("VIEW"), Command.ITEM, 30);
	}

	public void commandAction(Command arg0, Displayable arg1) {
		if (arg0 == cmdBack) {
			this.goBack();
		} else if (arg0 == cmdMap) {
			if (this.mapurl==null) {
				this.labInfo.setText("No map until download finishes.");
			} else {
				this.globalClass.showImage(this.mapurl);
			}
		}
	}
	
	private void cleanScreen() {
		this.deleteAll();
		//#style infoText
		this.labInfo = new StringItem(Locale.get("STATUS"), Locale.get("WAITMSG"));
		this.append(this.labInfo);		
	}
	
	public void search(String url) {
		this.cleanScreen();
		this.mapurl = null;
		String appended = url+"&offset="+String.valueOf(offset)+"&limit="+String.valueOf(limit);		
		this.dlManager.download(this, appended);
	}

	public void downloadCompleted(byte[] data) {
		String str = "";
		try {
			if (data==null || data.length<=0) {
				throw new Exception("Not found.");
			}
			str = GPSCookie.http2String(data);
			JSONObject json = new JSONObject(str);
			str = json.getString("msg");
			if ("OK".equals(str.toUpperCase())) {
				JSONArray items = json.getJSONArray("items");
				this.setItems(items);
				try {
					this.offset = json.getInt("offset");
					this.limit = json.getInt("limit");
				} catch (JSONException e) {	}
			} else {
				throw new Exception(str);
			}
		} catch (Exception e) {
			str = e.toString();
			this.labInfo.setText(str);
		}		
	}
	
	private void setItems(JSONArray items) throws Exception
	{
		this.cleanScreen();
		mapurl = "";
		for (int i=0; i<items.length() && i<=7; i++) {
			JSONObject item = items.getJSONObject(i);
			//#style recordhead
			StringItem record1 = new StringItem(item.getString("uuid"),Locale.get("LOCATION")+item.getString("lat") +","+ item.getString("lon"));
			//#style recordbody
			StringItem record2 = new StringItem("", item.getString("name"));
			record1.setItemCommandListener(this);
			record1.addCommand(this.cmdView);
			this.append(record1);
			this.append(record2);
			mapurl += item.getString("lat") +","+ item.getString("lon")+",midgreen|";
		}		
	}

	public void downloadError(Exception e) {
		this.labInfo.setText(e.getMessage());	
	}

	public void downloadStatus(int percent) {
		this.labInfo.setText(String.valueOf(percent)+"%");		
	}

	public void commandAction(de.enough.polish.ui.Command arg0, Item arg1) {
		String uuid = ((StringItem)arg1).getLabel();
		this.labInfo.setText(Locale.get("VIEW")+uuid);	
		this.globalClass.viewMission(uuid);
	}
}
